package statistics

import (
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/admin"
	"parking-lot-go-admin/model/work"

	"github.com/gin-gonic/gin"
)

func Question(c *gin.Context) {

	where := make(map[string]interface{})
	adminId, _ := c.Get("admin_id")
	if adminId != 1 {
		where["admin_id"] = adminId
	}

	var workOrderStruct work.WorkOrderStruct
	var workOrderData []work.WorkOrderStruct
	model.GetModelData(
		model.Db,
		workOrderStruct.TableName(),
		&workOrderData,
		model.QueryOptions{
			Page:       1,
			PageSize:   7,
			Conditions: where,
			OrderBy:    "id desc",
			Debug:      true,
		},
	)

	returnData := make([]map[string]interface{}, 0)
	for _, v := range workOrderData {
		status := "待处理"
		if v.Status == 1 {
			status = "已处理"
		}

		var adminStruct admin.AdminStruct
		answerUserName := ""
		if v.AnswerAdminId != 0 {
			answerUserName, _ = adminStruct.FindUser(v.AnswerAdminId)
		}

		questionUserName, _ := adminStruct.FindUser(v.AdminId)

		returnData = append(returnData, map[string]interface{}{
			"status":            status,
			"handle_at":         v.HandleAt.Format("2006-01-02 15:04"),
			"question":          v.Question,
			"answer":            v.Answer,
			"created_at":        v.CreatedAt.Format("2006-01-02 15:04"),
			"question_username": questionUserName,
			"answer_username":   answerUserName,
		})
	}

	controller.Success(c, returnData)
}
